#include <stdio.h>
#include <stdlib.h>
#include "E:\Desktop\data_struct\6Graph\2_Adjacency_Matrix\Adj_Matrix.h"

// 求点V到各点的最短路径
void DIJ(Adj_Matrix G, int v)
{
    int collect[MAX];
    int dis[MAX];
    int path[MAX];
    for (int i = 0; i < MAX; i++)
    {
        collect[i] = 0;
        dis[i] = INFINITY;
        path[i] = -1;
    }
    collect[v] = 1;
    dis[v] = 0;
    for (int w = 0; w < G.node_num; w++)
    {
        if (G.M[v][w] != 0 && G.M[v][w] != INFINITY)
        {
            dis[w] = G.M[v][w];
            path[w] = v;
        }
    }
    while (1)
    {
        int min = INFINITY + 1;
        int p;
        for (int w = 0; w < G.node_num; w++)
        {
            if (!collect[w] && dis[w] < min)
            {
                min = dis[w];
                p = w;
            }
        }
        if (min == INFINITY + 1)
            break;
        collect[p] = 1;
        for (int w = 0; w < G.node_num; w++)
        {
            if (!collect[w] && G.M[p][w] != 0 && G.M[p][w] != INFINITY && dis[p] + G.M[p][w] < dis[w])
            {
                dis[w] = dis[p] + G.M[p][w];
                path[w] = p;
            }
        }
    }

    for (int i = 0; i < G.node_num; i++)
    {
        printf("%3d", i);
    }
    printf("\n");

    for (int i = 0; i < G.node_num; i++)
    {
        printf("%3d", path[i]);
    }
    printf("\n");
    for (int i = 0; i < G.node_num; i++)
    {
        printf("%3d", dis[i]);
    }
}

int main()
{
    /*******************code*******************/
    Adj_Matrix G;
    creat_w(G);
    DIJ(G, 0);
    /******************************************/
    printf("\n\n****************************\n");
    printf("Press Enter key to continue\n");
    getchar();
    return 0;
    /******************************************/
}
